package sany.iec.config;

import org.openmuc.j60870.ClientConnectionBuilder;
import org.openmuc.j60870.Connection;
import org.openmuc.j60870.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sany.iec.service.IEC104Listener;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author : 托马斯
 * @Date : 2025/5/26 19:36
 * @Description :
 */
@Configuration
public class Iec104Client {

    @Value("${iec.server.ip}")
    private String ip;

    @Value("${iec.server.port}")
    private int port;

    @Resource
    private IEC104Listener iec104Listener;

    @Bean(name = "iec-connection")
    public Connection client() throws IOException {
        // 创建连接客户端
        Connection connection = new ClientConnectionBuilder(ip)
                .setPort(port)
                .setConnectionTimeout(1000)
                //.setReservedASduTypeDecoder(new CustomTypeDecoder()) // 自定义实现ASDU解码器
                .setConnectionEventListener(iec104Listener) // ASDU 数据接收监听
                .build();
        connection.startDataTransfer(); // 启动
        return connection;
    }

}
